package cn.tedu.medicalpath.patient.service.impl;

import cn.tedu.medicalpath.patient.mapper.PatientMapper;
import cn.tedu.medicalpath.patient.pojo.entity.Patient;
import cn.tedu.medicalpath.patient.pojo.param.DoctorQuery;
import cn.tedu.medicalpath.patient.pojo.param.PatientQuery;
import cn.tedu.medicalpath.patient.pojo.param.PatientSaveParam;
import cn.tedu.medicalpath.patient.pojo.vo.DoctorDepartmentVO;
import cn.tedu.medicalpath.patient.pojo.vo.DoctorVO;
import cn.tedu.medicalpath.patient.pojo.vo.PatientVO;
import cn.tedu.medicalpath.patient.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Override
    public PatientVO selectPatient(PatientQuery patientQuery) {
        //数据验证，查询参数是否为空,设置身份证号为必选项
        if (patientQuery == null || patientQuery.getPersonId() == null){
            return null;
        }
        Patient patient = patientMapper.selectPatient(patientQuery);
        if (patient == null){
            return null;
        }
        PatientVO patientVO = new PatientVO();
        BeanUtils.copyProperties(patient,patientVO);
        return patientVO;
    }

    @Override
    public Integer savePatient(PatientSaveParam patientSaveParam) {
        Patient patient = new Patient();
        BeanUtils.copyProperties(patientSaveParam,patient);
        Integer age = calculateAge(patient.getPersonId());
        patient.setAge(age);
        Integer id =null;
        //判断是修改还是新增
        if (patient.getId() == null){
            //新增
            id =patientMapper.insert(patient);
        }else {
            //修改
            id =patientMapper.updateById(patient);

        }
        return id;
    }

    @Override
    public List<DoctorDepartmentVO> selectDepartment() {
        //从数据库查询，医生对应的部门信息
        List<DoctorDepartmentVO> list =patientMapper.selectDepartment();
        return list;
    }

    @Override
    public List<DoctorVO> selectDoctor(DoctorQuery doctorQuery) {
        if (doctorQuery == null){
            return Collections.emptyList();
        }
        if (doctorQuery.getDepartment() == null){
            return Collections.emptyList();
        }
        //从数据库查询，部门对应的医生信息
        List<DoctorVO> doctorVOList = patientMapper.selectDoctor(doctorQuery);
        return doctorVOList;
    }

    //根据身份证号计算年龄
    private Integer calculateAge(String personId) {
        if (personId == null || personId.length() != 18) {
            throw new IllegalArgumentException("Invalid ID card length. It must be 18 digits.");
        }
        // 提取出生年月日部分（第7位到第14位）
        String birthDateString = personId.substring(6, 14);
        // 解析出生日期
        LocalDate birthDate = LocalDate.parse(birthDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 计算两个日期之间的年龄差
        Period period = Period.between(birthDate, currentDate);
        Integer age = period.getYears();
        return age;
    }

}



